﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SEO.Business;
using SEO.Entity;

/// <summary>
/// 查看收录信息
/// </summary>
public partial class ArticleSEOInfo : AdminPage
{
    /// <summary>
    /// WebSEOID
    /// </summary>
    protected int WebSEOID
    {
        get { return ViewState["WebSEOID"] == null ? 0 : Convert.ToInt32(ViewState["WebSEOID"]); }
        set { ViewState["WebSEOID"] = value; }
    }

    /// <summary>
    /// KeywordsID
    /// </summary>
    protected int KeywordsID
    {
        get { return ViewState["KeywordsID"] == null ? 0 : Convert.ToInt32(ViewState["KeywordsID"]); }
        set { ViewState["KeywordsID"] = value; }
    }

    /// <summary>
    /// KeywordsID1
    /// </summary>
    protected int KeywordsID1
    {
        get { return ViewState["KeywordsID1"] == null ? 0 : Convert.ToInt32(ViewState["KeywordsID1"]); }
        set { ViewState["KeywordsID1"] = value; }
    }

    /// <summary>
    /// 开始时间
    /// </summary>
    protected string BeginDateTime
    {
        get { return ViewState["BeginDateTime"] == null ? "" : ViewState["BeginDateTime"].ToString(); }
        set { ViewState["BeginDateTime"] = value; }
    }

    /// <summary>
    /// 结束时间
    /// </summary>
    protected string EndDateTime
    {
        get { return ViewState["EndDateTime"] == null ? "" : ViewState["EndDateTime"].ToString(); }
        set { ViewState["EndDateTime"] = value; }
    }

    /// <summary>
    /// ID
    /// </summary>
    protected int nID = 0;
    protected int nNum = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Common.GetQueryValue("id", 0), out nID);
        if (!Page.IsPostBack)
        {
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.ArticleSEO;
    }

    /// <summary>
    /// 页面初始化
    /// </summary>
    protected void InitData()
    {
        //获取信息
        BisArticleSEO bisArticleSEO = new BisArticleSEO();
        ArticleSEO Info = bisArticleSEO.Get(nID, nAdminUserID);
        if (Info != null)
        {
            WebSEOID = Info.WebSEOID.Value;
            if (Info.KeywordsID != null)
                KeywordsID = Info.KeywordsID.Value;
            if (Info.KeywordsID1 != null)
                KeywordsID1 = Info.KeywordsID1.Value;
            BeginDateTime=Info.CreateTime.Value.AddDays(1).ToString("yyyy-MM-dd 00:00:00");
            if (Info.AccountTime != null)
            {
                EndDateTime = Info.AccountTime.Value.ToString("yyyy-MM-dd 00:00:00");
            }
            else
            {
                SysConfig sysConfig = Common.GetSysConfig();
                EndDateTime = Info.CreateTime.Value.AddMonths(1).ToString("yyyy-MM-" + sysConfig.AccountDay.Value.ToString("00"));
            }
            InitList();

        }
        else
        {
            Response.Write("不存在");
            Response.End();
        }
    }

    protected void InitList()
    {
        //获取列表
        int nRecordCount = 0;
        int nPageCount = 0;
        BisArticleSEO bisInfo=new BisArticleSEO();
        DataTable dtSEO = new BisArticleSEO().GetArticleSEORelation(WebSEOID, KeywordsID,KeywordsID1,Convert.ToDateTime(BeginDateTime), Convert.ToDateTime(EndDateTime), anpPage.CurrentPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
        dtSEO.Columns.Add("SEO", typeof(string));
        if (dtSEO != null && dtSEO.Rows.Count > 0)
        {
            string strIDList = string.Empty;
            foreach (DataRow drSEO in dtSEO.Rows)
            {
                strIDList += (string.IsNullOrEmpty(strIDList) ? "" : ",") + drSEO["ID"].ToString();
            }
            if (!string.IsNullOrEmpty(strIDList))
            {
                DataTable dtDay = bisInfo.GetArticleDayList(strIDList);
                if (dtDay != null && dtDay.Rows.Count > 0)
                {
                    foreach (DataRow drSEO in dtSEO.Rows)
                    {
                        DataRow[] drList = dtDay.Select("KeysRelationID="+drSEO["ID"].ToString());
                        string strSEOInfo=string.Empty;
                        foreach (DataRow tem in drList)
                        {
                            strSEOInfo +=tem["CreateTime"].ToString()+ "<br/><b>第 " + tem["PageIndex"].ToString() + " 页第 " + tem["RowIndex"].ToString() + " 位 总排名 " + tem["AllIndex"].ToString() + " 位</b><br/>";
                        }
                        drSEO["SEO"] = strSEOInfo;
                    }
                }
                dtDay.Dispose();
            }
        }
        nNum = (anpPage.CurrentPageIndex - 1) * anpPage.PageSize;
        nNum++;
        anpPage.RecordCount = nRecordCount;
        if (nRecordCount == 0)
        {
            noData.Style["display"] = "block";
        }
        else
        {
            noData.Style["display"] = "none";
        }
        rptSEO.DataSource = dtSEO;
        rptSEO.DataBind();
        dtSEO.Dispose();
    }

    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="src"></param>
    /// <param name="e"></param>
    protected void anpPage_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        anpPage.CurrentPageIndex = e.NewPageIndex;
        InitData();
    }

}